package com.fenger.sys.service.impl;

import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.domain.User;
import com.fenger.sys.mapper.RoleMapper;
import com.fenger.sys.mapper.UserMapper;
import com.fenger.sys.service.UserService;
import com.fenger.sys.utils.DataGirdView;
import com.fenger.sys.utils.Md5Utils;
import com.fenger.sys.vo.UserVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;

    @Override
    @Transactional(readOnly = true)
    public Integer queryUserMaxOrderNum() {
        return userMapper.queryUserMaxOrderNum();
    }

    @Override
    @Transactional(readOnly = true)
    public DataGirdView queryAllUser(UserVo userVo) {
        Page<Object> objects = PageHelper.startPage(userVo.getPage(), userVo.getLimit());
        List<User> user = this.userMapper.queryAllUser(userVo);
        return new DataGirdView(SysConstast.SUCCESS_CODE, SysConstast.SUCCESS_MSG, objects.getTotal(), user);
    }

    @Override
    public void addUser(UserVo userVo) {
        this.userMapper.insertSelective(userVo);
    }

    @Override
    public void updateUser(UserVo userVo) {
        this.userMapper.updateByPrimaryKeySelective(userVo);
    }

    @Override
    public void deleteUser(Integer id) {
        this.userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void resetUserPwd(UserVo userVo) {

    }

    @Override
    public void dispatcherUserRole(UserVo userVo) {

    }

    @Override
    @Transactional(readOnly = true)
    public User selectByLoginname(String loginname) {
        return this.userMapper.selectByLoginname(loginname);
    }

    @Override
    public void updateUserPwd(UserVo userVo) throws Exception {
        User user = this.userMapper.selectByLoginname(userVo.getLoginname());
        if (null != user) {
            String pwd = user.getPwd();
            String oldpwd = userVo.getPwd();
            String newpwd = userVo.getNewpwd();
            Object salt = user.getSalt();
            String oldMd5Pwd= Md5Utils.md5Password(oldpwd, salt);
            String newMd5Pwd= Md5Utils.md5Password(newpwd, salt);

            if (!oldMd5Pwd.equals(pwd)) {
                throw new Exception(SysConstast.PASSWORD_ERROR_MSG);
            }else {
                user.setPwd(newMd5Pwd);
                this.userMapper.updateByPrimaryKeySelective(user);
            }
        } else {
            throw new Exception(SysConstast.NOT_LOGINNAME_ERROR_MSG);
        }

    }


    @Override
    @Transactional(readOnly = true)
    public List<User> queryUserByDeptId(Integer deptid) {
        return this.userMapper.queryUserByDeptId(deptid);
    }

    @Override
    @Transactional(readOnly = true)
    public User queryUserById(Integer id) {
        return this.userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveUserRole(Integer id, Integer[] rids) {
        //删除用户原有的所有角色
        this.roleMapper.deleteRoleByUserid(id);

        //添加用户的角色
        if (null != rids && rids.length > 0)
            for (Integer rid : rids) {
                this.roleMapper.insertRoleUser(id, rid);
            }
    }


}
